﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMSMVC4.Areas.Admin.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin,Introduce")]
    public class GioiThieuController : Controller
    {
        //
        // GET: /Admin/GioiThieu/
        CMSMVC4Entities db = new CMSMVC4Entities();

        public ActionResult Index()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/GioiThieu").Rows[0]["Id"];
            Category cate = db.Categories.FirstOrDefault(x => x.Type == MenuType.GioiThieu);
            if (cate == null)
            {
                Category ctn = new Category();
                ctn.Status = CategoriStatus.Thuong;
                ctn.Type = MenuType.GioiThieu;
                ctn.Sort = 1;
                db.Categories.AddObject(ctn);
                db.SaveChanges();
                cate = db.Categories.FirstOrDefault(x => x.Type == MenuType.GioiThieu);
                CateLang ctl = new CateLang();
                ctl.CategoriesId = cate.Id;
                ctl.LanguageCode = "vi-vn";
                ctl.Name = "Giới Thiệu";
                db.CateLangs.AddObject(ctl);
                ctl.CategoriesId = cate.Id;
                ctl.LanguageCode = "en-us";
                ctl.Name = "Introduction";
                db.CateLangs.AddObject(ctl);
                db.SaveChanges();
            }
            Page page = db.Pages.FirstOrDefault(x => x.CategoriesID == cate.Id);
            if (page == null)
            {
                Page pn = new Page();
                pn.CategoriesID = cate.Id;
                pn.Status = PageStatus.GioiThieu;
                pn.Tab = 1;
                pn.View = 0;
                db.Pages.AddObject(pn);
                db.SaveChanges();
                page = db.Pages.FirstOrDefault(x => x.CategoriesID == cate.Id);
            }
            PageLang pagelangv = db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("vi-vn"));
            PageLang pagelange = db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("en-us"));
            if (pagelangv == null)
            {
                PageLang pgn = new PageLang();
                pgn.Name = "Giới Thiệu";
                pgn.LanguageCode = "vi-vn";
                pgn.PageId = page.Id;
                db.PageLangs.AddObject(pgn);
                db.SaveChanges();
                pagelangv = db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("vi-vn"));
            }
            if (pagelange == null)
            {
                PageLang pgn = new PageLang();
                pgn.Name = "Introduction";
                pgn.LanguageCode = "en-us";
                pgn.PageId = page.Id;
                db.PageLangs.AddObject(pgn);
                db.SaveChanges();
                pagelange = db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("en-us"));
            }
            GioiThieuModel model = new GioiThieuModel();
            model.id = page.Id;

            model.Pageidv = pagelangv.Id;
            model.fullv = pagelangv.FullContent;
            model.sumv = pagelangv.Summary;

            model.Pageide = pagelange.Id;
            model.fulle = pagelange.FullContent;
            model.sume = pagelange.Summary;
            return View("Index",model);
        }

        [HttpPost]
        public ActionResult Index(GioiThieuModel model)
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/GioiThieu").Rows[0]["Id"];
            if (ModelState.IsValid)
            {
                PageLang pagelangv = db.PageLangs.FirstOrDefault(x => x.Id == model.Pageidv && x.LanguageCode.Equals("vi-vn"));
                PageLang pagelange = db.PageLangs.FirstOrDefault(x => x.Id == model.Pageide && x.LanguageCode.Equals("en-us"));
                if (pagelangv != null)
                {
                    pagelangv.FullContent = model.fullv;
                    pagelangv.Summary = model.sumv;
                    db.SaveChanges();
                }
                if (pagelange != null)
                {
                    pagelange.FullContent = model.fulle;
                    pagelange.Summary = model.sume;
                    db.SaveChanges();
                }
                ModelState.AddModelError("", "Lưu dữ liệu thành công");
            }
            if (Request.IsAjaxRequest())
            {
                return Json("Lưu dữ liệu thành công", JsonRequestBehavior.AllowGet);
            }
            return View(model);
        }
    }
}
